package com.maxqaq.service.impl;

import com.maxqaq.constant.MessageConstant;
import com.maxqaq.entity.Goods;
import com.maxqaq.entity.EasyUser;
import com.maxqaq.mapper.GoodsMapper;
import com.maxqaq.mapper.UserMapper;
import com.maxqaq.result.PageResult;
import com.maxqaq.result.QueryInfo;
import com.maxqaq.result.Result;
import com.maxqaq.service.GoodsService;
import com.maxqaq.utils.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result findPage(QueryInfo queryInfo) {
        PageHelper.startPage(queryInfo.getPageNumber(), queryInfo.getPageSize());
        Page<Goods> page = goodsMapper.findPage(queryInfo.getQueryString());
        if (page == null) {
            return new Result(false, MessageConstant.GOODS_SELECT_FAIL);
        }
        long total = page.getTotal();
        List<Goods> result = page.getResult();
        return new Result(true, MessageConstant.GOODS_SELECT_SUCCESS, new PageResult(result, total));
    }

    @Override
    public Result add(Goods goods) {
        Goods good = goodsMapper.findByName(goods.getName());
        if (good != null) {
            return new Result(false, MessageConstant.GOODS_EXIST);
        }
        int i = goodsMapper.insert(goods);
        if (i <= 0) {
            return new Result(false, MessageConstant.ADD_GOODS_FAIL);
        }
        return new Result(true, MessageConstant.ADD_GOODS_SUCCESS);
    }

    @Override
    public Result delete(Integer id) {
        if (id == null) {
            return new Result(false, MessageConstant.CHOOSE_GOODS);
        }
        int i = goodsMapper.delete(id);
        if (i <= 0) {
            return new Result(false, MessageConstant.DELETE_GOODS_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_GOODS_SUCCESS);
    }

    @Override
    public Result edit(Goods goods) {
        int update = goodsMapper.update(goods);
        if (update <= 0) {
            return new Result(false, MessageConstant.UPDATE_GOODS_FAIL);
        }
        return new Result(true, MessageConstant.UPDATE_GOODS_SUCCESS);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result batchImport(List<Goods> list, Integer userId) {
        EasyUser easyUser = userMapper.findById(userId);
        if (easyUser == null) {
            return new Result(false, MessageConstant.ADMIN_NOT_LOGIN);
        }
        int i = 0,j = 0;
        try {
            for (Goods goods : list) {
                Goods byName = goodsMapper.findByName(goods.getName());
                if (byName != null) {
                    goods.setId(byName.getId());
                    goods.setRemark(byName.getRemark() + "； 管理员：" + easyUser.getNickName() + "于" + DateUtil.getNowDateTime() + " --> 更新了商品:" + goods.getName() + "；目前数量：" + goods.getNumber());
                    i += goodsMapper.update(goods);
                } else {
                    goods.setCreateTime(new Date());
                    goods.setCreateUserId(userId);
                    goods.setRemark("管理员：" + easyUser.getNickName() + "于" + DateUtil.getNowDateTime() + "将商品：" + goods.getName() + "加入库存，数量：" + goods.getNumber());
                    j += goodsMapper.insert(goods);
                }
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.BATCH_IMPORT_FAIL);
        }
        if (i <= 0 && j <= 0) {
            return new Result(false, MessageConstant.BATCH_IMPORT_FAIL);
        }
        return new Result(true, MessageConstant.BATCH_IMPORT_SUCCESS);
    }

    @Override
    public List<Goods> findAll() {
        return goodsMapper.findAll();
    }
}
